<?php
class User extends CI_Controller {

function __construct()
    {
    	parent::__construct();
    	if($this->session->userdata('logged_in')==TRUE){
			$this->load->helper(array('form','url'));
        	$this->load->library('form_validation');
        	$this->load->model('user_model');
		}else{
			redirect('/index.php/authentication_controller');
		}	
     }
	function index(){
		$this->load->view('users_list_view');
	}
	
	function listUsers(){
		
			$this->load->view('users_list_view');
		
	}
	
	function getUsers(){
		$page = $this->input->post('page');
		$limit = $this->input->post('rows');
		$sidx = $this->input->post('sidx');
		$sord = $this->input->post('sord');
		
		if(!$sidx) $sidx =1;
		
		$count = $this->user_model->get_users_count();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		
		$users = $this->user_model->get_users($sidx, $sord, $start , $limit); 
		
		for($i=0; $i< sizeof($users); $i++) {
			$responce->rows[$i]['id'] = $users[$i]['id'];
			$responce->rows[$i]['cell'] = array($users[$i]['id'], 
												$users[$i]['username'], 
												$users[$i]['firstname'],
												$users[$i]['lastname'], 
												$users[$i]['email'],'action');
		}
     	
		echo json_encode($responce);
	}
	
	
	function createUser(){
		$data['message']=" ";
		$this->load->view('createUser',$data);
		
	}
    function logout(){
    	$this->session->set_userdata('validation',FALSE);
    	$this->session->set_userdata('logged_in',FALSE); 
    	$data['loginErr']=false;
	    $this->load->view('login_view',$data);
    }
    
	function editUser(){
		$user = $_POST['idUser'];
		$var=  $this->user_model->getUser($user);
		$data['name'] = $var[0]['firstname'];
		$data['last_name'] = $var[0]['lastname'];
        $data['id'] = $var[0]['id'];
        $data['username']=$var[0]['username'];
		$data['tel']=$var[0]['tel'];
		$data['dir']=$var[0]['dir'];
		$data['language']=$var[0]['language'];
		$data['password']=$var[0]['password'];
		$data['email']=$var[0]['email'];
		$data['country']=$var[0]['country'];
		$data['message']=" ";
		$this->load->view('editUser',$data);
	}
	function update(){
		$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|max_length[100]|xss_clean');
		$this->form_validation->set_rules('name','Name','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('last_name','Last_name','required|trim|max_length[50]|xss_clean');		
		if ($this->form_validation->run() == false) {
			$data['message']="User creation failure !";;
			$this->load->view('users_list_view');			
		} else {
			$id = $this->input->post('id');
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$name=$this->input->post('name');
			$last_name=$this->input->post('last_name');
			$email=$this->input->post('email');
			$tel=$this->input->post('tel');
			$dir=$this->input->post('dir');
			$lan=$this->input->post('language');
			$country=$this->input->post('country');
			$id=$this->User_Model->update_user($id,$username,$password,$name,$last_name,
			$email,$tel,$dir,$lan,$country);
			if (! $id){
			   $this->session->set_userdata('validation',TRUE);
			   $data['message']="user edit failure !";
			   $this->load->view('users_list_view');		
			}else{
			   $this->load->view('UsersList');
			}
				
		}
		
	}
	
	function login(){
		//Check incoming variables
		$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|max_length[100]|xss_clean');
				
		if ($this->form_validation->run() == false) {
			$data['loginErr']=false;
			$this->session->set_userdata('validation',FALSE);
			$this->load->view('login_view',$data);			
		} else {
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$id=$this->User_Model->check_login($username,$password);
			$data['loginErr']=false;
			if (! $id){
				//login failure	
				$data['loginErr']=true;
				$this->load->view('login_view',$data);
			}else{
				//logged
				$this->session->set_flashdata('login_error',FALSE);
				$this->session->set_userdata('logged_in',TRUE);
				$this->session->set_userdata('user_id',$id);
				$this->session->set_userdata('username',$username);
				$this->session->set_userdata('validation',TRUE);
				$this->load->view('tasks_list_view');
			}		
		}
	}
	
  	public function save(){
   		$id			= $this->input->post('id');
   		$username 	= $this->input->post('username');
		$password	= $this->input->post('password');
		$firstname	= $this->input->post('firstname');
		$lastname	= $this->input->post('lastname');
		$email		= $this->input->post('email');
		$tel		= $this->input->post('tel');
		$address	= $this->input->post('address');
		$language	= $this->input->post('language');
		$country	= $this->input->post('country');
		
		if($id != NULL && $id != ''){
			$this->user_model->update($id, $username, $password, $firstname, $lastname, $email, $tel, $address, $language, $country);
		}
		else{
			$insertId = $this->user_model->create($username, $password, $firstname, $lastname, $email, $tel, $address, $language, $country);
		}
		$data["page"]=3;
		$this->load->view('main_menu_view',$data);
   	}
	
	
	function upsert_user(){
		$this->load->helper('url');
		$this->load->library('form_validation');
		//Check incoming variables
		//$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        //$this->form_validation->set_rules('password','Password','required|trim|max_length[100]|xss_clean');
		//$this->form_validation->set_rules('name','Name','required|trim|max_length[50]|xss_clean');
        //$this->form_validation->set_rules('last_name','Last_name','required|trim|max_length[100]|xss_clean');		
		//if ($this->form_validation->run() == false) {
			//$this->session->set_userdata('validation',FALSE);
			//$data['message']="User creation failure !";;
			//$this->load->view('users_list_view');			
		//} else {
			$idUser = $this->input->post('id');
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$name=$this->input->post('name');
			$last_name=$this->input->post('last_name');
			$email=$this->input->post('email');
			$tel=$this->input->post('tel');
			$dir=$this->input->post('dir');
			$lan=$this->input->post('language');
			$country=$this->input->post('country');
			
			if($idUser != NULL && $id != ''){
				$id=$this->User_Model->update($id,$username,$password,$name,$last_name,
				$email,$tel,$dir,$lan,$country);		}
			else{
			
				$id=$this->User_Model->create($username,$password,$name,$last_name,
				$email,$tel,$dir,$lan,$country);
			}
			//if (! $id){	
			  // $this->session->set_userdata('validation',TRUE);
			   //$data['message']="user creation failure !";
			   //$this->load->view('users_list_view');	
			//}else{
				//created
			   $this->load->view('users_list_view');
			//}
		//}
	}
	function create(){
		$this->load->helper('url');
		$this->load->library('form_validation');
		//Check incoming variables
		$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|max_length[100]|xss_clean');
		$this->form_validation->set_rules('name','Name','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('last_name','Last_name','required|trim|max_length[100]|xss_clean');		
		if ($this->form_validation->run() == false) {
			$this->session->set_userdata('validation',FALSE);
			$data['message']="User creation failure !";;
			$this->load->view('createUser',$data);			
		} else {
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$name=$this->input->post('name');
			$last_name=$this->input->post('last_name');
			$email=$this->input->post('email');
			$tel=$this->input->post('tel');
			$dir=$this->input->post('dir');
			$lan=$this->input->post('language');
			$country=$this->input->post('country');
			$id=$this->User_Model->create_user($username,$password,$name,$last_name,
			$email,$tel,$dir,$lan,$country);
			if (! $id){
				
			   $this->session->set_userdata('validation',TRUE);
			   $data['message']="user creation failure !";
			   $this->load->view('createUser',$data);	
			}else{
				//created
			   $this->load->view('UsersList');
			}
				
		}
	}
	function delete(){
		$user = $_POST['idUser'];
		$this->user_model->remove($user);
		$this->load->view('users_list_view');
	}
	
	function get_json_users_autocomplete(){
		$term 	= $this->input->get('term');
		$users 	= $this->user_model->get_like_users($term);
		
		$auto_complete_users = array();
		
		for($i=0; $i< sizeof($users); $i++) {
			$auto_complete_users[$i]['key'] = $users[$i]['id'];
			$auto_complete_users[$i]['value'] = $users[$i]['username'];
		}
		
		echo json_encode($auto_complete_users);
	}
	function get(){
		$id = $_POST['id'];
		$result = NULL;
		
		if($id != NULL && $id != ''){
			$user = $this->user_model->get($id);
			$result = $user[0];
		}
		echo json_encode($result);
	}
}
?>